package View;

/**
 * Creates an inside view of the elevator panel with the available destinations
 * open door and close door options
 * When a destination is chosen the request is sent to the Dispatcher
 * 
 * @author Varun Maryada, Sanat Dixit, Steve Tannahill
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;

import javax.swing.*;

import Dispatcher.SendElevatorRequestThread;

@SuppressWarnings("serial")
public class ElevatorGUI extends JFrame implements ActionListener {

	private int id;
	
	private JLabel elevatorName;
	private JButton floor1;
	private JButton floor2;
	private JButton floor3;
	private JButton floor4;
	private JButton floor5;
	private JButton floor6;
	private JButton floor7;
	private JButton floor8;
	private JButton floor9;
	private JButton floor10;
	private JButton floor11;
	private JButton floor12;
	private JButton floor13;
	private JButton floor14;
	private JButton floor15;

	private JButton open;
	private JButton close;

	private JPanel elevatorPanel;

	private PrintWriter elevatorController;
	/**
	 * Constructs Elevator Panel
	 * 
	 * @param number
	 * @param gd
	 */
	public ElevatorGUI(int number, PrintWriter pw) {
		super("Elevator "+number);
		this.id = number;
		this.elevatorController = pw;
		this.elevatorName = new JLabel("Elevator:" + this.id, JLabel.CENTER);
		elevatorPanel = new JPanel();
		this.setLayout(new BorderLayout());
		this.setSize(300, 300);
		
		this.elevatorPanel.setLayout(new GridLayout(8, 1));
		this.initButtons();
		this.add(elevatorPanel, BorderLayout.CENTER);
	}

	/**
	 * Creates Floor Buttons
	 */
	public void initButtons() {
		elevatorPanel.add(new JLabel());
		elevatorPanel.add(elevatorName);
		elevatorPanel.add(new JLabel());
		floor1 = new JButton("1");
		floor1.addActionListener(this);
		elevatorPanel.add(floor1);
		floor2 = new JButton("2");
		floor2.addActionListener(this);
		elevatorPanel.add(floor2);
		floor3 = new JButton("3");
		floor3.addActionListener(this);
		elevatorPanel.add(floor3);
		floor4 = new JButton("4");
		floor4.addActionListener(this);
		elevatorPanel.add(floor4);
		floor5 = new JButton("5");
		floor5.addActionListener(this);
		elevatorPanel.add(floor5);
		floor6 = new JButton("6");
		floor6.addActionListener(this);
		elevatorPanel.add(floor6);
		floor7 = new JButton("7");
		floor7.addActionListener(this);
		elevatorPanel.add(floor7);
		floor8 = new JButton("8");
		floor8.addActionListener(this);
		elevatorPanel.add(floor8);
		floor9 = new JButton("9");
		floor9.addActionListener(this);
		elevatorPanel.add(floor9);
		floor10 = new JButton("10");
		floor10.addActionListener(this);
		elevatorPanel.add(floor10);
		floor11 = new JButton("11");
		floor11.addActionListener(this);
		elevatorPanel.add(floor11);
		floor12 = new JButton("12");
		floor12.addActionListener(this);
		elevatorPanel.add(floor12);
		floor13 = new JButton("13");
		floor13.addActionListener(this);
		elevatorPanel.add(floor13);
		floor14 = new JButton("14");
		floor14.addActionListener(this);
		elevatorPanel.add(floor14);
		floor15 = new JButton("15");
		floor15.addActionListener(this);
		elevatorPanel.add(floor15);

		open = new JButton("<||>");
		open.addActionListener(this);
		elevatorPanel.add(open);
		elevatorPanel.add(new JLabel());
		close = new JButton(">||<");
		close.addActionListener(this);
		elevatorPanel.add(close);
	}

	//Elevator call goes directly from the GUI to the
	//elevator controller, does not go through the dispatcher
	public void processElevatorCall(int destination) {
		if (this.elevatorController == null) {
			System.out.println("Cannot establish connection with the elevator");
		}
		SendElevatorRequestThread srt = new SendElevatorRequestThread(
				destination, this.elevatorController);
		srt.start();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == close) {
			this.processElevatorCall(-1);
		} else if (e.getSource() == open) {
			this.processElevatorCall(0);
		} else {
			this.processElevatorCall(Integer.parseInt(((JButton) e
					.getSource()).getText()));
		}

	}

}
